#include "Server.h"
using std::string;

namespace android::wmark
{
    Server::Server(/* args */)
    {
        mBufManager = new BufManager();
        mFreetype = new FreetypeWrapper();
    }

    Server::~Server()
    {
        if (mBufManager)
        {
            delete (mBufManager);
            mBufManager = NULL;
        }

        if (mFreetype)
        {
            delete (mFreetype);
            mFreetype = NULL;
        }
    }

    Return<void> Server::init(int32_t bufNum)
    {
        return Void();
    }

    Return<void> Server::updatePicture(const WmarkPicture &picture)
    {
        sendPng(picture);
        return Void();
    }

    Return<void> Server::updateText(const WmarkText &text)
    {
        sendText(text);
        return Void();
    }

    Return<void> Server::release()
    {
        return Void();
    }

    int Server::sendText(const WmarkText &text)
    {
        wmark_buf_t msg;
        int wmarkBufId = text.locate.index;
        string wmark_str = text.text;
        uint64_t endLen = 0;
        
        int w = text.w;
        int h = text.h;

        uint8_t *strBuf = (uint8_t *)malloc(w * h);
        if (!strBuf) {
            ALOGE("%s %d strBuf == NULL fail", __func__, __LINE__);
            return -1;
        }

        memset(strBuf, 0, w*h);

        mFreetype->setFont(text.font.size, text.font.padding_x, text.font.padding_y);
        mFreetype->text2img(wmark_str, strBuf, w, h, endLen);

        msg.info.wmark_buf_id = wmarkBufId;
        msg.info.picture.type = TYPE_STR;
        msg.info.picture.display.ch = 1;
        msg.info.picture.display.en = text.locate.enable;
        msg.info.picture.display.w = w;
        msg.info.picture.display.h = h;
        msg.info.picture.display.len = w * h;
        msg.info.picture.display.x = text.locate.x;
        msg.info.picture.display.y = text.locate.y;

        msg.info.picture.display.bg.r = text.bg.r;
        msg.info.picture.display.bg.g = text.bg.g;
        msg.info.picture.display.bg.b = text.bg.b;
        msg.info.picture.display.bg.a = text.bg.a;

        msg.info.picture.display.face_id.id1 = text.locate.faceId[0];
        msg.info.picture.display.face_id.id2 = text.locate.faceId[1];
        msg.info.picture.display.face_id.id3 = text.locate.faceId[2];
        msg.info.picture.display.face_id.id4 = text.locate.faceId[3];

        ALOGD("%s %d : wmark_str=%s wmark_buf_id:%d w:%d h:%d ch:%d x:%d y:%d en:%d faceId:%d %d %d %d", __func__, __LINE__, wmark_str.c_str(), wmarkBufId,
             w, h, 1, text.locate.x, text.locate.y, text.locate.enable,
             text.locate.faceId[0], text.locate.faceId[1], text.locate.faceId[2], text.locate.faceId[3]);

        mBufManager->sendNotify(strBuf, endLen, msg);

        if (strBuf) {
            free(strBuf);
            strBuf = NULL;
        }
        return 0;
    }

    int Server::sendPng(const WmarkPicture &picture)
    {
        wmark_buf_t msg;
        int wmarkBufId = picture.locate.index;
        
        int w = picture.w;
        int h = picture.h;
        int ch = picture.ch;
        int64_t buf_len = w * h * ch;

        if (buf_len > MEM_200KB) {
            ALOGE("%s %d : w.h.ch %d.%d.%d > 200KB error", __func__, __LINE__, w, h, ch);
            return -1;
        }

        msg.info.wmark_buf_id = wmarkBufId;
        msg.info.picture.type = TYPE_PNG;
        msg.info.picture.display.ch = ch;
        msg.info.picture.display.en = picture.locate.enable;
        msg.info.picture.display.w = w;
        msg.info.picture.display.h = h;
        msg.info.picture.display.len = w * h * ch;
        msg.info.picture.display.x = picture.locate.x;
        msg.info.picture.display.y = picture.locate.y;

        msg.info.picture.display.face_id.id1 = picture.locate.faceId[0];
        msg.info.picture.display.face_id.id2 = picture.locate.faceId[1];
        msg.info.picture.display.face_id.id3 = picture.locate.faceId[2];
        msg.info.picture.display.face_id.id4 = picture.locate.faceId[3];

        ALOGD("%s %d : wmark_buf_id:%d w:%d h:%d ch:%d x:%d y:%d en:%d faceId:%d %d %d %d", __func__, __LINE__, wmarkBufId,
             w, h, ch, picture.locate.x, picture.locate.y, picture.locate.enable,
             picture.locate.faceId[0], picture.locate.faceId[1], picture.locate.faceId[2], picture.locate.faceId[3]);

        mBufManager->sendNotify((uint8_t *)&picture.data[0], w * h * ch, msg);

        return 0;
    }
}
